﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Frame.Lib.Utilities
{
    public class CommUtil
    {
        public static Image ThumbImage(Image image, int width, int height)
        {
            Image imgNew = new Bitmap(width, height);
            Graphics graph = Graphics.FromImage(imgNew);
            graph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            graph.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);

            image.Dispose();

            //graph.DrawImage(image, 0, 0, image.Width, image.Height);

            return imgNew;


            ////从文件取得图片对象，并使用流中嵌入的颜色管理信息
            //System.Drawing.Image myImage = System.Drawing.Image.FromStream(fromFileStream, true);
            ////缩略图宽、高
            //System.Double newWidth = myImage.Width, newHeight = myImage.Height;
            ////宽大于模版的横图
            //if (myImage.Width > myImage.Height || myImage.Width == myImage.Height)
            //{
            //    if (myImage.Width > templateWidth)
            //    {
            //        //宽按模版，高按比例缩放
            //        newWidth = templateWidth;
            //        newHeight = myImage.Height * (newWidth / myImage.Width);
            //    }
            //}
            ////高大于模版的竖图
            //else
            //{
            //    if (myImage.Height > templateHeight)
            //    {
            //        //高按模版，宽按比例缩放
            //        newHeight = templateHeight;
            //        newWidth = myImage.Width * (newHeight / myImage.Height);
            //    }
            //}
            ////取得图片大小
            //System.Drawing.Size mySize = new Size((int)newWidth, (int)newHeight);
            ////新建一个bmp图片
            //System.Drawing.Image bitmap = new System.Drawing.Bitmap(mySize.Width, mySize.Height);
            ////新建一个画板
            //System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            ////设置高质量插值法
            //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            ////设置高质量,低速度呈现平滑程度
            //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            ////清空一下画布
            //g.Clear(Color.White);
            ////在指定位置画图
            //g.DrawImage(myImage, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
            //new System.Drawing.Rectangle(0, 0, myImage.Width, myImage.Height),
            //System.Drawing.GraphicsUnit.Pixel);
            /////文字水印
            ////System.Drawing.Graphics G=System.Drawing.Graphics.FromImage(bitmap);
            ////System.Drawing.Font f=new Font("宋体",10);
            ////System.Drawing.Brush b=new SolidBrush(Color.Black);
            ////G.DrawString("myohmine",f,b,10,10);
            ////G.Dispose();
            /////图片水印
            ////System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));
            ////Graphics a = Graphics.FromImage(bitmap);
            ////a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
            ////copyImage.Dispose();
            ////a.Dispose();
            ////copyImage.Dispose();
            ////保存缩略图
            //bitmap.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
            //g.Dispose();
            //myImage.Dispose();
            //bitmap.Dispose();

            //另外的方法


            //var img = Image.FromFile(strPath);        // 如果不是图片会出错。
            //if (img.Width <= intWidth)
            //{
            //    img.Dispose();
            //    return;
            //}
            //var intHeight = img.Height * intWidth / img.Width;
            //// 创建位图及相关联的图形处理工具，在位图上画缩略图
            //var thm = new Bitmap(intWidth, intHeight);
            //var grp = Graphics.FromImage(thm);
            //grp.DrawImage(img, 0, 0, intWidth, intHeight);
            //// 释放占用的图片文件
            //img.Dispose();
            //grp.Dispose();
            //// 设置图片质量
            //var ep = new EncoderParameters(1);
            //ep.Param[0] = new EncoderParameter(Encoder.Quality, lngQuality);
            //// 保存缩略图
            //// thm.Save(strPath);        // 如果不设置图片质量，可直接保存
            //thm.Save(strPath, ImageCodecInfo.GetImageEncoders().FirstOrDefault(i => i.MimeType == "image/jpeg"), ep);
            //thm.Dispose();


        }

        #region 验证数字
        /// <summary>
        /// 验证数字
        /// </summary>
        /// <param name="p_strNumber"></param>
        /// <returns></returns>
        public static bool IsNumeric(string p_strNumber)
        {
            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"^-?\d*\.?\d+$");
            bool blnReturn = reg.IsMatch(p_strNumber);
            return blnReturn;
        }
        #endregion

        /// <summary>
        /// 合并列表并去重
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <param name="userIDs"></param>
        /// <param name="list"></param>
        /// <param name="p"></param>
        public static void MergeList<T>(List<T> src, List<T> added)
        {
            foreach (T t in added)
            {
                if (src.Contains(t))
                {
                    continue;
                }
                src.Add(t);
            }
        }
    }
}
